<?php
require_once("../data/common.php");

$variables['USERNAME'] = "";
$variables['EMAIL'] = "";
$variables['EMAIL_CONFIRM'] = "";

if (!empty($_POST)) {
	$variables['ERROR'] = "";
	if (empty($_POST['username'])) 
		$variables['ERROR'] .= "You need to enter a username<br />";
	else 	$username = sanitize($_POST['username']);
	if (empty($_POST['email'])) 
		$variables['ERROR'] .= "You need to enter a email address<br />";
	else 	$email = sanitize($_POST['email']);
	if (empty($_POST['email_confirm'])) 
		$variables['ERROR'] .= "You need to enter your email address for the second time<br />";
	else 	$email_confirm = sanitize($_POST['email_confirm']);

	if ($_POST['email_confirm'] != $_POST['email']) 
		$variables['ERROR'] .= "Email addresses differ, please correct<br />";

	if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST["email"]))
		$variables['ERROR'] .= "Not a valid email address<br />";

	if (strlen($_POST["username"]) < 2)
		$variables['ERROR'] .= "Your username needs to be 2 or more letters long.<br />";

		if (!isset($_POST["tos"])) {
			$variables['ERROR'] .= "You need to accept the Terms Of Service to proceed.<br />";
		}

	$variables['USERNAME'] = $username;
	$variables['EMAIL'] = $email;
	$variables['EMAIL_CONFIRM'] = $email_confirm;


	if (empty($variables['ERROR'])) {
		if ($user->user_exists($username, $email)) {
			$variables['ERROR'] .= "Username and/or email address you selected already exists on our system<br />";
		} else {
			if ( $user->new_user($username, $email) ) {
				$template = new STE();
				$template->load("signup_done");
				$template->assign($variables);
				print $template->display();
				exit;
			} else {
				$variables['ERROR'] .= "Oops! Something broke down.<br />";
				
			}
		}
	}

}
$template = new STE();
$template->load("signup");
$variables['SITE_URL'] = $site_url;
$template->assign($variables);
print $template->display();

?>
